// 导入用户集合
const { User } = require('../../../model/user');
const { generateToken } =  require('../../../middleGuard/jwt.js')

module.exports = async (req, res) => {
	const { username, password, nickname } = req.body
  const user = await User.findOne({username})
  if (!user) {
    User.create({
      username,
      password,
      nickname,
      createTime: new Date().getTime()
    }).then(item => {
      console.log(item)
      res.json({
          data: {
            ...item.toObject(),
            token: generateToken({userId: item._id})
          },
          message: '创建用户成功！'
      })
    }).catch((err) => {
      res.status(403)
      res.json({
          log: err,
          message: '用户创建失败！'
      })
    })
  } else {
    res.status(403)
    res.json({
        message: '用户名已存在！'
    })
  }
}